package com.rent.common.config;

import com.rent.common.constant.CommonConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类
 * 定义交换机、队列和绑定关系
 */
@Configuration
public class RabbitMQConfig {

    /**
     * 消息转换器
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 配置 RabbitTemplate 使用 JSON 转换器
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(messageConverter());
        return template;
    }

    // 在 RabbitMQConfig 中添加
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(messageConverter());
        factory.setConcurrentConsumers(3);  // 设置并发消费者数量
        factory.setMaxConcurrentConsumers(10);
        return factory;
    }
    
    /**
     * 定义Topic交换机
     */
    @Bean
    public Exchange rentExchange() {
        return ExchangeBuilder
                .topicExchange(CommonConstant.EXCHANGE_NAME)
                .durable(true)
                .build();
    }
    
    // ==================== 房源审核相关 ====================
    
    /**
     * 房源审核队列
     */
    @Bean
    public Queue houseAuditQueue() {
        return QueueBuilder
                .durable(CommonConstant.QUEUE_HOUSE_AUDIT)
                .build();
    }
    
    /**
     * 绑定房源审核队列到交换机
     */
    @Bean
    public Binding houseAuditBinding() {
        return BindingBuilder
                .bind(houseAuditQueue())
                .to(rentExchange())
                .with(CommonConstant.ROUTING_KEY_HOUSE_AUDIT)
                .noargs();
    }
    
    // ==================== 预约通知相关 ====================
    
    /**
     * 预约通知队列
     */
    @Bean
    public Queue appointmentQueue() {
        return QueueBuilder
                .durable(CommonConstant.QUEUE_APPOINTMENT)
                .build();
    }
    
    /**
     * 绑定预约通知队列到交换机
     */
    @Bean
    public Binding appointmentBinding() {
        return BindingBuilder
                .bind(appointmentQueue())
                .to(rentExchange())
                .with(CommonConstant.ROUTING_KEY_APPOINTMENT)
                .noargs();
    }
    
    // ==================== 申请通知相关 ====================
    
    /**
     * 申请通知队列
     */
    @Bean
    public Queue applyQueue() {
        return QueueBuilder
                .durable(CommonConstant.QUEUE_APPLY)
                .build();
    }
    
    /**
     * 绑定申请通知队列到交换机
     */
    @Bean
    public Binding applyBinding() {
        return BindingBuilder
                .bind(applyQueue())
                .to(rentExchange())
                .with(CommonConstant.ROUTING_KEY_APPLY)
                .noargs();
    }

    /**
     * 申请聊天消息队列
     */
    @Bean
    public Queue chatMessageQueue() {
        return QueueBuilder
                .durable(CommonConstant.CHAT_MESSAGE)
                .build();
    }

    /**
     * 绑定申请通知队列到交换机
     */
    @Bean
    public Binding chatMessageBinding() {
        return BindingBuilder
                .bind(chatMessageQueue())
                .to(rentExchange())
                .with(CommonConstant.ROUTING_KEY_CHAT_MESSAGE)
                .noargs();
    }
}

